package com.mianyang.zhangsan.controller;

import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.entity.SecAnswer;
import com.mianyang.zhangsan.service.SecAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/answer")
public class SecAnswerController extends BaseController {
    @Autowired
    private SecAnswerService answerService;

    /**
     * 查询试题答案
     */
    @GetMapping("/{problemId}")
    public R queryProblemAnswer(@PathVariable("problemId") Long problemId) {
        List<SecAnswer> answers = answerService.selectAnswerByProblemId(problemId);
        return success("answers", answers);
    }

    /**
     * 修改试题答案
     */
    @PutMapping("/edit")
    public R editProblemAnswer(@RequestBody SecAnswer answer) {
        answerService.updateById(answer);
        return success();
    }

    /**
     * 删除答案
     */
    @DeleteMapping("/delete/{answerId}")
    public R deleteAnswer(@PathVariable("answerId") Long answerId) {
        answerService.removeById(answerId);
        return success();
    }

    /**
     * 新增答案
     */
    @PostMapping("/save")
    public R saveAnswer(@RequestBody SecAnswer answer) {
        answerService.save(answer);
        return success();
    }
}
